package game.module.ddz.processor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.ddz.bean.DdzRound;
import game.module.ddz.logic.DdzDataManager;
import game.session.SessionManager;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageDdz.C2SDdzJiaBei;
import lua.LuaMessageDdz.PushDdzJiaBei;
import lua.LuaMessageDdz.PushShowDi;
import lua.LuaMessageDdz.S2CDdzJiaBei;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SDdzJiaBei.id, accessLimit = 500)
public class DdzJiaBeiProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(DdzJiaBeiProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw request) throws Exception {
		C2SDdzJiaBei reqMsg = C2SDdzJiaBei.parse(request);
		int playerId = playingRole.getId();
		byte is_jiabei = reqMsg.is_jiabei;
		logger.info("ddz jia bei,playerId={},is_jiabei={}", playerId, is_jiabei);
		// 房间是否存在
		DdzRound ddzRound = DdzDataManager.getInstance().getDdzRoom(playerId);
		if (ddzRound == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 还没产生地主
		if (ddzRound.getDiZhuIndex() == null) {
			S2ErrorCode respMsg = new S2ErrorCode(210);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 是否已经加倍
		if (ddzRound.isJiaBei()) {
			S2ErrorCode respMsg = new S2ErrorCode(213);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 是否为闲家
		int dizhuIndex = ddzRound.getDiZhuIndex();
		int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
		if (myIndex == dizhuIndex) {
			S2ErrorCode respMsg = new S2ErrorCode(214);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// do
		Map<Integer, Integer> jiaBeiMap = ddzRound.getJiaBeiMap();
		if (jiaBeiMap == null) {
			jiaBeiMap = new HashMap<>();
			ddzRound.setJiaBeiMap(jiaBeiMap);
		}
		if (is_jiabei == 1) {
			jiaBeiMap.put(myIndex, 2);
			sendResp(playingRole, ddzRound, playerId, is_jiabei);
		} else {
			jiaBeiMap.put(myIndex, 1);
			sendResp(playingRole, ddzRound, playerId, is_jiabei);
		}
		if (jiaBeiMap.size() >= ddzRound.getPlayerIds().size() - 1) {
			ddzRound.setJiaBei(true);
			pushShowDi(ddzRound);
		}
	}

	private void pushShowDi(DdzRound ddzRound) {
		// 底牌移动到玩家牌上
		int diZhuIndex = ddzRound.getDiZhuIndex();
		List<Byte> myCards = ddzRound.getCards().get(diZhuIndex);
		myCards.addAll(ddzRound.getDi());
		// push
		PushShowDi pushMsg = new PushShowDi();
		pushMsg.di_cards = ArrayUtility.toByteArray(ddzRound.getDi());
		List<Integer> playerIds = ddzRound.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
	}

	private void sendResp(PlayingRole playingRole, DdzRound ddzRound, int playerId, byte is_qiang) {
		S2CDdzJiaBei retMsg = new S2CDdzJiaBei();
		playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
		// push
		int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
		PushDdzJiaBei pushMsg = new PushDdzJiaBei((byte) myIndex, is_qiang);
		List<Integer> playerIds = ddzRound.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
